其他
Sui 极速上手 | Move dApp 极速入门(拾贰)
本文对 Sui 的官方文档有所参考:
https://docs.sui.io
Sui, a next-generation smart contract platform with high throughput, low latency, and an asset-oriented programming model powered by Move.
0x01 安装 Sui 的二进制程序集
1)检查 Rust
版本,如果版本太低,可以先升级:
$ rustc --version
rustc 1.65.0 (897e37553 2022-11-02)
$ cargo --version
cargo 1.65.0 (4bc8f24d3 2022-10-20)
2)下载 repo:
$ git clone https://github.com/MystenLabs/sui.git --branch devnet
3)切换到 dev 分支
$ cd sui
$ git checkout devnet
4)安装 binaries
$ cargo install --locked --git https://github.com/MystenLabs/sui.git --branch "devnet" sui
5)检验安装结果
$ sui
0x02 账户操作
1)激活账户
$ sui client active-address
会得到一个账户以及其对应的Secret Recovery Phrase
。
2)账户切换
$ sui client switch --address 0x913cf36f370613ed131868ac6f9da2420166062e
3)领取测试币(Faucet)
在 discord 的 #devnet-faucet 频道领取:
https://discord.com/channels/916379725201563759/971488439931392130
发送如下指令:
!faucet [你的地址]
4)查看余额
$ sui client gas
5)查看对象合集
$ sui client objects
6)查看单个对象
$ sui client object [object id]
0x03 创建 NFT
$ sui client create-example-nft
我们可以在浏览器中查到创建的示例 NFT:
https://explorer.sui.io/address/[your_address]
0x04 合约操作
https://docs.sui.io/devnet/build/move
1)创建合约项目
$ sui move new my_first_package
2)在 sources
文件夹中新建my_module.move
文件:
module my_first_package::my_module {
// Part 1: imports
use sui::object::{Self, UID};
use sui::transfer;
use sui::tx_context::{Self, TxContext};
// Part 2: struct definitions
struct Sword has key, store {
id: UID,
magic: u64,
strength: u64,
}
struct Forge has key, store {
id: UID,
swords_created: u64,
}
// Part 3: module initializer to be executed when this module is published
fun init(ctx: &mut TxContext) {
let admin = Forge {
id: object::new(ctx),
swords_created: 0,
};
// transfer the forge object to the module/package publisher
transfer::transfer(admin, tx_context::sender(ctx));
}
// Part 4: accessors required to read the struct attributes
public fun magic(self: &Sword): u64 {
self.magic
}
public fun strength(self: &Sword): u64 {
self.strength
}
public fun swords_created(self: &Forge): u64 {
self.swords_created
}
// part 5: public/ entry functions (introduced later in the tutorial)
// part 6: private functions (if any)
}
3)编译合约
$ cd my_new_package
$ sui move build
4)部署合约
$ sui client publish ./ --gas [gas-object] --gas-budget 30000 --verify-dependencies